<html>
<head>
    <meta charset="UTF-8">
    <title>应用模型信息内部服务列表</title>
    <link id="easyuiTheme"  href="../../static/easyui/themes/bootstrap/easyui.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/easyui/themes/icon.css" rel="stylesheet" type="text/css"/>
    <link href="../../static/css/esaas-easyui-add.css" rel="stylesheet" type="text/css"/>
    <script src="../../static/easyui/jquery.min.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.cookie.js" type="text/javascript"></script>
    <script src="../../static/easyui/ui-theme.js" type="text/javascript"></script>
    <script src="../../static/easyui/jquery.easyui.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../static/easyui/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.util.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.jquery.ajax.js" type="text/javascript"></script>
    <script src="../../static/plugin/assets/jslib.easyui.util.js" type="text/javascript"></script>
    <script src="../../ffres/dict/all.js" type="text/javascript"></script>
    <a href="" target="_blank" id="commonLink" name="commonLink"></a>
</head>
<script type="text/javascript">
    var appId = $.getUrlParam('appId');
    function onDblClickRow(index,row) {
        window.parent.onChooseInnerService(row);
        window.parent.JLEUtil.closewin();
    }

    function selectReturn() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            onDblClickRow(-1, row);
        } else {
            $.messager.alert('提示', '请选择一条记录！', 'info');
        }
    }

    function closeForm() {
        window.parent.JLEUtil.closewin();
    }


    function reloadList(packageCode, appModelCatalog,packageId) {
        if(packageId=="001"){
            packageId=null;
        }
        $('#dg').datagrid({
            url: `../../api/app/model/infos`,
            queryParams:{
                'packageId':packageId,
                'appId': appId
                // 'appModelCatalog':appModelCatalog,
                // 'tableModelCode': packageCode
            }
        });
    };

    function reloadTree() {
        var appModelCatalogs = ["I00", "I01", "I11", "I91"];
        $('#nav').tree({
            url:'../../api/app/model/tree/nav/' + $.getUrlParam('appId') + '/catalog',
            queryParams : {
                appModelCatalogs : appModelCatalogs.join(",")
            },
            onClick: function(node){
                var parent = $('#nav').tree('getParent', node.target);
                reloadList(node.attributes.package_code, node.attributes.app_model_catalog,node.id)
            }
        })
    }
    $(function () {
        reloadTree();
    });
</script>
</head>

<body class="easyui-layout">

    <div data-options="region:'west',title:'导航栏'" style="width:283px;padding:1px;">
    <ul id="nav" class="easyui-tree"
                data-options="animate:true,
                    lines:true,method:'post',
                    animate:true">
    </ul>
    </div>
    <div data-options="region:'north',title:''" style="height: 40px; background: #F4F4F4;">
        <form id="searchForm">
            <table>
                <tr>
                    <td>&nbsp;服务名：</td>
                    <td><input name="appModelCode" id="appModelCode" /></td>
                    <td>&nbsp;服务描述：</td>
                    <td><input name="appModelName" id="appModelName" /></td>
                    <td><a class="easyui-linkbutton" href="javascript:void(0);"
                           onclick="JLEUtil.queryForm('searchForm','dg','../../api/app/model/infos?');">查找</a>
                    </td>
                    <td><a class="easyui-linkbutton" href="javascript:void(0);"
                           onclick="JLEUtil.clearForm('searchForm');">清空</a></td>
                </tr>
            </table>
        </form>
    </div>
    <div data-options="region:'center',split:false">
        <table id="dg" class="easyui-datagrid" title="" style="width:100%;height:90%"
               data-options="singleSelect:true,collapsible:true,method:'post'" toolbar="#tb" title="Load Data"            rownumbers="true" pagination="true" pageSize="20">
            <thead>
                <tr>
                    <th data-options="field:'appModelId',resizable:true">服务标识</th>
                    <th data-options="field:'appModelCode',resizable:true">服务名</th>
                    <th data-options="field:'appModelName',resizable:true">服务描述</th>
                    <th data-options="field:'appModelCatalog',resizable:true" formatter="formatter_APP_MODEL_CATALOG">
                       服务分类</th>
                    <th data-options="field:'appModelType',resizable:true" formatter="formatter_APP_MODEL_TYPE">服务类型
                    </th>
                    <th data-options="field:'packageId',resizable:true">目录包标识</th>
                    <th data-options="field:'tableModelId',resizable:true">表模型ID</th>
                    <th data-options="field:'tableModelCode',resizable:true">表模型代码</th>
                    <th data-options="field:'tableModelName',resizable:true">表模型名称</th>
                    <th data-options="field:'createUser',resizable:true">创建人</th>
                    <th data-options="field:'createTime',resizable:true">创建时间</th>
                    <th data-options="field:'lastUpdateUser',resizable:true">最后修改人</th>
                    <th data-options="field:'lastUpdateTime',resizable:true">最后更新时间</th>
                </tr>
            </thead>
        </table>
    </div>
    <div id="tb" style="padding:1px">
        <div style="margin-bottom:2px">
            <a href="#" class="easyui-menubutton" data-options="menu:'#infoMenu',iconCls:'icon-add'" plain="true">下拉按钮</a>
            <a href="javascript:doAdd()" class="easyui-linkbutton" iconCls="icon-add" plain="true">登记服务</a>
            <a href="javascript:doUpdate()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改服务</a>
            <a href="javascript:doDelete()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除服务</a>
            <a href="javascript:doView()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">查看服务</a>
            <a href="javascript:generateExcelDocuments()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">下载导入模板</a>
            <a href="javascript:openImportDataDialog()" class="easyui-linkbutton" iconCls="icon-ok" plain="true">导入服务清单</a>
        </div>
    </div>
    </div>
    <div id="infoMenu" style="width: 150px;">
        <div onclick="javascript:doAddTree()">创建下级目录</div>
        <div onclick="javascript:doUpdateTree()">修改当前目录</div>
        <div onclick="javascript:doDeleteTree()">删除当前目录</div>
        <div onclick="javascript:doViewTree()">查看当前目录</div>
    </div>
    <div id="dd"  class="easyui-dialog" title="导入Excel文档数据" style="width:400px;height:200px;" data-options="closed:true,resizable:true,modal:true">
            <form id="fileUpload" method="post" enctype="multipart/form-data" action="../../api/app/model/info/external/upload">
                <table class="esaas-info-add-table">
                    <tr></tr>
                    <tr>
                        <td width="100%"><input  class="easyui-filebox" id="file" name="file" data-options="required:false,buttonText:'选择文件'" style="width: 100%;height: 25px" /></td>
                    </tr>
                    <tr>
                        <td width="100%"><label id="fileName" /></td>
                    </tr>
                </table>
                <div>
                </div>
            </form>
        <div style="text-align: center; padding: 15px">
            <a href="javascript:void(0)" class="easyui-linkbutton" id="importDataButton" onclick="importData()">&nbsp;保&nbsp;存&nbsp;</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="closeDialog()">&nbsp;关&nbsp;闭&nbsp;</a>
        </div>

    </div>
</body>
<script>
    var appId = $.getUrlParam('appId');
    var projectId = $.getUrlParam('projectId');
    var packageCode = $.getUrlParam('packageCode');

    function openImportDataDialog(){
        var node = $('#nav').tree('getSelected');
        if (node==null){
            $.messager.alert('提示','请选择服务目录!','info');
        }else {
            $("#dd").window('open')
            $('#importDataButton').linkbutton('disable')
        }
    }
    $(function (){
//选择文件时，检验是否为excel文件
        $('#file').filebox({
            onChange:function (newValue,oldValue){
                //获取上传文件控件内容
                //filebox_file_id_1 这个ID 是easyui 自己创建的input 便签. 这里面是真正保存文件的地方.
                //如果创建了多个filebox  那么后面的ID 就是filebox_file_id_2,filebox_file_id_3.
                file = document.getElementById('filebox_file_id_2').files[0];
                // 判断控件中是否存在文件内容，如果不存在，弹出提示信息，阻止进一步操作
                if (file == null) { alert('错误，请选择文件'); return; }
                //获取文件名称
                var fileName = file.name;
                //获取文件类型名称
                var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
                //这里限定上传文件文件类型必须为.xlsx或.xls，如果文件类型不符，提示错误信息
                if (file_typename == '.xlsx'||file_typename == '.xls') {
                    //计算文件大小
                    var fileSize = 0;
                    //如果文件大小大于1024字节X1024字节，则显示文件大小单位为MB，否则为KB
                    if (file.size > 1024 * 1024) {
                        fileSize = Math.round(file.size * 100 / (1024 * 1024)) / 100;

                        if (fileSize > 100) {
                            alert('错误，文件超过100MB，禁止上传！');
                            return;
                        }
                        fileSize = fileSize.toString() + 'MB';
                    } else {
                        fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
                    }
                    $('#importDataButton').linkbutton('enable')
                }
                else {
                    alert("文件类型错误，请选择excel文件");
                    //将错误信息显示在前端label文本中
                    $('#importDataButton').linkbutton('disable')
                }
            }
        })
    })
    function importData(){
        $('#importDataButton').linkbutton('disable')
        var node = $('#nav').tree('getSelected');
        var packageId = node.id;
        $('#fileUpload').form('submit',{
            url: '../../api/app/model/info/external/upload/'+appId+'/'+packageId,
            onSubmit: function(){
                document.getElementById('fileName').innerHTML = "<span style='color:Blue'>导入中，请等待完成！</span>";
            },
            success: function(result){
                document.getElementById('fileName').innerHTML = "<span style='color:Blue'>"+result+"</span>";
            }
        });
    }
    function closeDialog(){
        $("#dd").window('close')
    }


    function doAddTree() {
        var node = $('#nav').tree('getSelected');
        JLEUtil.createModalWin('新增目录', `AppModelTreeAdd.html?appId=${appId}&packageAbvId=${node.id}`);
    }

    function doDeleteTree() {
        var node = $('#nav').tree('getSelected');
        if (node.children && node.children.length > 0) {
            $.messager.alert('提示','存在下级目录则提示不可以删除！','info');
        } else {
            $.messager.confirm('删除提示', '你确定要删除这条记录?', function(r){
                if (r){
                    var jsonData = JSON.parse('{'+'"packageId":"' + node.id + '"'+'}');
                    $.restfulDelete('../../api/app/model/tree/', jsonData, function(data) {
                        $.messager.alert('提示','记录删除成功!','info');
                        $('#nav').tree('remove', node.target);
                    });
                }
            });
        }
    }

    function doUpdateTree() {
        var node = $('#nav').tree('getSelected');
        if (node) {
            JLEUtil.createModalWin('修改目录', `AppModelTreeUpdate.html?appId=${appId}&packageId=${node.id}`);
        } else {
            $.messager.alert('提示', '请选择一条记录！', 'info');
        }
    }

    function doViewTree() {
        var node = $('#nav').tree('getSelected');
        if (node) {
            JLEUtil.createModalWin('查看目录', `AppModelTreeView.html?appId=${appId}&packageId=${node.id}`);
        } else {
            $.messager.alert('提示', '请选择一条记录！', 'info');
        }
    }

    function doAdd() {
        var node = $('#nav').tree('getSelected');
        JLEUtil.createModalWin('新增', `AppModelInfoNew.html?appId=${appId}&projectId=${node.id}&appModelCatalog=A01&packageCode=${packageCode}`);
    }


    function doUpdate() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            JLEUtil.createModalWin('修改目录', 'IsvInoutInfoUpdate.html?appId='+appId+'&appModelId='+row.appModelId);
        } else {
            $.messager.alert('提示', '请选择一条记录！', 'info');
        }
    }

    function doView() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            JLEUtil.createModalWin('修改目录', `IsvInoutInfoView.html?appId=${appId}&appModelId=${row.appModelId}`);
        } else {
            $.messager.alert('提示', '请选择一条记录！', 'info');
        }
    }

    function doDelete() {
        var row = $('#dg').datagrid('getSelected');
        if (row) {
            $.messager.confirm('删除提示', '你确定要删除这条记录?', function(r){
                if (r){
                    var jsonData = JSON.parse('{'+'"appModelId":"' + row.appModelId + '"'+'}');
                    $.restfulDelete('../../api/app/model/info', jsonData, function(data) {
                        $.messager.alert('提示','记录删除成功!','info');
                        $('#dg').datagrid('reload');
                    });
                }
            });
        } else {
            $.messager.alert('提示','请选择一条记录！','info');
        }
    }

    function generateExcelDocuments() {
        location.href = "../../api/app/model/info/external/excel/"+appId;
    }

</script>
</html>